define(function () {
    'use strict';

    function ctrl($scope,$timeout,$filter,$ionicPopup,$ionicActionSheet,$stateParams,
                  LocalCacheService,InfoService,WebService,$ionicLoading,CommonService) {

        $scope.channel = {};
        $scope.vm = {};
        $scope.vm.activeTab=1;

        $scope.$on('$ionicView.beforeEnter', function() {
            init();
        });

        $scope.doRefresh = function() {
            init().finally(function(){
                $scope.$broadcast('scroll.refreshComplete');
            });
        };

        function init(){
            if(!$scope.profitFunds){
                $ionicLoading.show();
            }
            $scope.showFilterSel = false;
            $scope.companys = new Array();
            $scope.channel.channelId = $stateParams['id'];
            //查询广告
            $scope.showAds();

            return InfoService.getProductsByChannelId($scope.channel.channelId).then(
                function(funds) {
                    $scope.funds =  funds;
                    for(var i=0;i<$scope.funds.length;i++){
                        var fund = $scope.funds[i];
                        fund.prod_min_subscribe = $filter('prodMinSubscribe')(CommonService.getMinAmount(fund));

                        var company = {};
                        company.org_code = fund.org_code;
                        company.org_level = fund.org_level;
                        company.org_name = fund.org_name;
                        company.org_pic_url = fund.org_pic_url;
                        company = addCompany(company);
                        if(!company.funds) {
                            company.funds = new Array();
                        }
                        company.funds.push(fund);
                    }
                    $ionicLoading.hide();
                }
            );
        }

        function addCompany(company){
            for(var i = 0; i < $scope.companys.length; i++) {
                if($scope.companys[i].org_code == company.org_code) {
                    return $scope.companys[i];
                }
            }
            $scope.companys.push(company);
            return $scope.companys[$scope.companys.length -1];
        }


        $scope.showAds = function(type){
            WebService.wtAd("xcm_stockTeam_1").then(function(data){
                if(data) {
                    $scope.ads = data;
                    var adKey = "ad_" + $scope.channel.adId;
                    if((!LocalCacheService.get(adKey) || type) && $scope.ads.length > 0) {
                        var myPopup = $ionicPopup.show({
                            cssClass: "kDialog",
                            templateUrl: "product/ads.html",
                            scope: $scope,
                            buttons: [
                                { text: '确定' }
                            ]
                        });
                        LocalCacheService.set(adKey, true);
                    }
                }
            });
        }

        $scope.show = function() {
            var hideSheet= $ionicActionSheet.show({
                cancelOnStateChange:true,
                cssClass:'action_s',
                titleText: "排序",
                buttons: [
                    { text: $scope.vm.activeTab == 1 ? "<b>按收益排序</b>" : "按收益排序" },
                    { text: $scope.vm.activeTab == 2 ? "<b>按基金评级排序</b>" : "按基金评级排序" },
                    { text: $scope.vm.activeTab == 3 ? "<b>按基金公司评级排序</b>" : "按基金公司评级排序" },

                ],
                buttonClicked: function(index) {
                    $scope.vm.activeTab= index+1;
                    return true;
                },
                cancelText: "取消",
                cancel: function() {
                    console.log('执行了取消操作');
                    return true;
                }
            });
            $timeout(function() {
                hideSheet();
            }, 4000);
        };
    }

    ctrl.$inject = ['$scope','$timeout','$filter','$ionicPopup','$ionicActionSheet','$stateParams',
        'LocalCacheService','InfoService','WebService','$ionicLoading','CommonService'];
    return ctrl;
});